#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-*-
# UbiNote - A Lightweight Note-talking Application
#
# Copyright (C) 2006-2009 Shellex <5h3ll3x@gmail.com>
#
# UbiNote is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# UbiNote is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with UbiNote; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA


from dbmap import note
from dbmgr import dbmgr
class notemgr:
    @staticmethod
    def add(title):
        c = dbmgr.cursor()
        c.execute('''INSERT INTO note (title) VALUES(?);''',
            (title,));
        dbmgr.commit();
        return c.lastrowid

    @staticmethod
    def remove(id):
        c = dbmgr.cursor()
        c.execute('''DELETE FROM note WHERE note.id=?''', (id,))
        dbmgr.commit()
        pass

    @staticmethod
    def search(title):
        pass

    @staticmethod
    def count():
        return len(dbmgr.cursor().execute('select * from note').fetchall())

    @staticmethod
    def rename(id, title):
        c = dbmgr.cursor()
        c.execute('''UPDATE note SET title=? WHERE note.id=?''', (title, id))
        dbmgr.commit()
        return id 

    @staticmethod
    def get(id):
        c = dbmgr.cursor()
        n = c.execute('''select * from note 
            where note.id=?''',
            (id,)).fetchone()
        return note(n)

    @staticmethod
    def get_all():
        c = dbmgr.cursor()
        notes = []
        raws = c.execute('select * from note').fetchall()
        for e in raws:
            n = note(e)
            notes.append(n)
        return notes




